package com.chengyu.core.service.ty.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.mapper.TyQuoteMapper;
import com.chengyu.core.mapper.TyShopMapper;
import com.chengyu.core.model.*;
import com.chengyu.core.service.ty.TyQuoteService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TyQuoteServiceImpl implements TyQuoteService {


    @Autowired
    private TyQuoteMapper quoteMapper;

    @Autowired
    private TyShopMapper shopMapper;

    @Override
    public Boolean add(UmsMember member, String orderNo, BigDecimal price, String priceDesc) throws ServiceException {
        TyShopExample shopExample = new TyShopExample();
        shopExample.createCriteria().andMemberIdEqualTo(member.getId());
        List<TyShop> shops = shopMapper.selectByExample(shopExample);
        if (CollectionUtil.isEmpty(shops)) {
            throw new ServiceException("商家不存在");
        }
        TyShop shop = shops.get(0);
        TyQuote tyQuote = new TyQuote();
        tyQuote.setName(shop.getName());
        tyQuote.setType(shop.getType());
        tyQuote.setService(shop.getService());
        tyQuote.setLevel(shop.getLevel());
        tyQuote.setKeyword(shop.getKeyword());
//        tyQuote.setMemberId(shop.getMemberId());
        tyQuote.setMemberId(member.getId());
        tyQuote.setMemberName(shop.getMemberName());
        tyQuote.setSellerStar(shop.getSellerStar());
        tyQuote.setLogo(shop.getLogo());
        tyQuote.setProduce(shop.getProduce());
        tyQuote.setPrice(price);
        tyQuote.setPriceDesc(priceDesc);
        tyQuote.setOrderNo(orderNo);
//        System.out.println(tyQuote);
        quoteMapper.insertSelective(tyQuote);
        return Boolean.TRUE;
    }

    @Override
    public Boolean add(UmsMember member, String orderNo, Map<String, BigDecimal> map_price, String priceDesc) throws ServiceException {
        Date now = DateUtil.date();

//        TyShopExample shopExample = new TyShopExample();
//        shopExample.createCriteria().andMemberIdEqualTo(member.getId());
//        List<TyShop> shops = shopMapper.selectByExample(shopExample);
//        if (CollectionUtil.isEmpty(shops)) {
//            throw new ServiceException("商家不存在");
//        }
//        TyShop shop = shops.get(0);
        TyQuote tyQuote = new TyQuote();
        tyQuote.setMemberId(member.getId());
//        tyQuote.setName(shop.getName());
//        tyQuote.setType(shop.getType());
//        tyQuote.setService(shop.getService());
//        tyQuote.setLevel(shop.getLevel());
//        tyQuote.setKeyword(shop.getKeyword());
//        tyQuote.setMemberId(shop.getMemberId());
//        tyQuote.setMemberName(shop.getMemberName());
//        tyQuote.setSellerStar(shop.getSellerStar());
//        tyQuote.setLogo(shop.getLogo());
//        tyQuote.setProduce(shop.getProduce());
        tyQuote.setPrice(map_price.get("1"));
        tyQuote.setPrice1(map_price.get("2"));
        tyQuote.setPrice2(map_price.get("3"));
        tyQuote.setPrice3(map_price.get("4"));
        tyQuote.setPrice4(map_price.get("5"));
        tyQuote.setPrice5(map_price.get("6"));


        tyQuote.setPriceDesc(priceDesc);
        tyQuote.setOrderNo(orderNo);


        tyQuote.setAddTime(now);
        tyQuote.setUpdTime(now);


        quoteMapper.insertSelective(tyQuote);
        return Boolean.TRUE;
    }


//    @Override
//    public Boolean add(UmsMember member, String orderNo, String price, String priceDesc) throws ServiceException {
//        TyShopExample shopExample = new TyShopExample();
//        shopExample.createCriteria().andMemberIdEqualTo(member.getId());
//        List<TyShop> shops = shopMapper.selectByExample(shopExample);
//        if (CollectionUtil.isEmpty(shops)) {
//            throw new ServiceException("商家不存在");
//        }
//        TyShop shop = shops.get(0);
//        TyQuote tyQuote = new TyQuote();
//        tyQuote.setName(shop.getName());
//        tyQuote.setType(shop.getType());
//        tyQuote.setService(shop.getService());
//        tyQuote.setLevel(shop.getLevel());
//        tyQuote.setKeyword(shop.getKeyword());
//        tyQuote.setMemberId(shop.getMemberId());
//        tyQuote.setMemberName(shop.getMemberName());
//        tyQuote.setSellerStar(shop.getSellerStar());
//        tyQuote.setLogo(shop.getLogo());
//        tyQuote.setProduce(shop.getProduce());
//        tyQuote.setPrice(price);
//        tyQuote.setPriceDesc(priceDesc);
//        tyQuote.setOrderNo(orderNo);
//        quoteMapper.insertSelective(tyQuote);
//        return Boolean.TRUE;
//    }

    @Override
    public List<TyQuote> getQuoteList(String orderNo, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        TyQuoteExample quoteExample = new TyQuoteExample();
        quoteExample.createCriteria().andOrderNoEqualTo(orderNo);
        //使用全文检索
        return quoteMapper.selectByExample(quoteExample);
    }


    @Override
    public long getQuoteCount(String orderNo,Integer memberId) {
//        PageHelper.startPage(page, pageSize);
        TyQuoteExample quoteExample = new TyQuoteExample();
        quoteExample.createCriteria().andOrderNoEqualTo(orderNo).andMemberIdEqualTo(memberId);
        //使用全文检索
        return quoteMapper.countByExample(quoteExample);
    }


    @Override
    public Long getQuoteCount(String orderNo) {
        TyQuoteExample quoteExample = new TyQuoteExample();
        quoteExample.createCriteria().andOrderNoEqualTo(orderNo);
        //使用全文检索
        return quoteMapper.countByExample(quoteExample);
    }


}
